package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Role;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.FileUtil;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @DubboReference
    private AdminService adminService;

    @DubboReference
    private RoleService roleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping
    public String index(HttpServletRequest request, Model model) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return "admin/index";
    }

    //跳转到新增页面
    @GetMapping("/create")
    public String create() {
        return "admin/create";
    }

    //实现用户新增
    @PostMapping("/save")
    public String save(Admin admin) {
        admin.setHeadUrl("https://img.zcool.cn/community/019ffa56ed7e0f32f875a944c54810.jpg@1280w_1l_2o_100sh.jpg");
        //使用SpringSecurity所使用的加密器对admin的密码进行加密
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return "common/successPage";
    }

    /**
     * 用户修改
     * 先要根根据id查询用户信息并进行回显
     * 再进行修改提交
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Admin admin = adminService.getById(id);
        model.addAttribute("admin", admin);
        return "admin/edit";
    }

    @PostMapping("/update")
    public String update(Admin admin) {
        adminService.update(admin);
        return "common/successPage";
    }

    //删除用户
    @GetMapping("delete/{id}")
    public String delete(@PathVariable Long id) {
        adminService.delete(id);
        return "redirect:/admin";
    }

    //显示用户管理的上传头像页面
    @GetMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable("id") Long id, Model model) {
        model.addAttribute("id", id);
        return "admin/upload";
    }

    //上传头像
    @PostMapping("/upload/{id}")
    public String upload(@PathVariable("id") Long id, @RequestParam("file") MultipartFile multipartFile) throws IOException {
        //将图片传到七牛云
        //1.获取一个唯一的文件名
        String uuidName = FileUtil.getUUIDName(multipartFile.getOriginalFilename());
        //2.获取一个文件的存储路径
        String dateDirPath = FileUtil.getDateDirPath();
        String fileName = dateDirPath + uuidName;
        QiniuUtil.upload2Qiniu(multipartFile.getBytes(), fileName);
        //3.获取文件的访问路径
        String url = QiniuUtil.getUrl(fileName);
        //4.根据id获取当前用户信息
        Admin admin = adminService.getById(id);
        admin.setHeadUrl(url);
        //5.修改用户信息
        adminService.update(admin);
        return "common/successPage";
    }

    //分配角色
    @GetMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable("adminId") Long adminId, Model model) {
        //1.调用业务层方法查询用户已分配和未分配的角色列表
        Map<String, List<Role>> roleListMap = roleService.findRoleListMapByadminId(adminId);
        //2.将查询到的数据存储到请求域,h会将map中的所有K-V作为请求域的键值对存起来
        model.addAllAttributes(roleListMap);
        //3.将用户id存储到请求域
        model.addAttribute("adminId", adminId);
        //4.显示视图
        return "admin/assignShow";
    }

/*    //用户分配角色
    @PostMapping("/assignRole")
    public String assignRole(@RequestParam("adminId") Long adminId,
                             @RequestParam("roleIds") List<Long> roleIds) {
        //adminId就是当前要分配角色的用户id
        //roleIds就是要分配给用户的那些角色的id集合
        //调用业务层方法给用户分配角色
        roleService.saveAdminRole(adminId, roleIds);

        //显示成功页面
        return "common/successPage";
    }*/

    //用户分配角色
    @PostMapping("/assignRole")
    public String assignRole(@RequestParam("adminId") Long adminId,
                             @RequestParam("roleIds") Long[] roleIds) {
        //adminId就是当前要分配角色的用户id
        //roleIds就是要分配给用户的那些角色的id集合
        //调用业务层方法给用户分配角色
        roleService.saveAdminRole(adminId, roleIds);

        //显示成功页面
        return "common/successPage";
    }
}
